<template>
  <common-list>
    <common-search slot="search" @search="handleQuery" @reset="resetQuery">
      <el-form
        ref="queryForm"
        :inline="true"
        :model="queryParams"
        @submit.native.prevent
        label-width="40px"
      >
        <el-form-item label="接口" prop="url">
          <common-input
            :verify="false"
            v-model="queryParams.url"
            @enter="handleQuery"
            clearable
            :maxlength="256"
          />
        </el-form-item>
        <el-form-item label="时间" prop="time">
          <el-date-picker
            popper-class="timePopper"
            v-model="date"
            type="datetimerange"
            @change="dateChange"
            align="right"
            unlink-panels
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :picker-options="pickerOptions"
            :default-time="['00:00:00', '23:59:59']"
            :clearable="false"
            :editable="false"
          >
          </el-date-picker>
        </el-form-item>
        <!-- <el-form-item label="访问时间">
          <el-date-picker
            v-model="date"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            value-format="yyyy-MM-dd"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </el-form-item> -->
      </el-form>
    </common-search>
    <common-table
      slot="content"
      v-loading="loading"
      :data="list"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        label="业务系统名称"
        align="left"
        prop="businessName"
        width="200"
        fixed="left"
        show-overflow-tooltip
      />
      <el-table-column
        label="应用名称"
        align="left"
        prop="appName"
        show-overflow-tooltip
        width="200px"
      />
      <el-table-column
        label="接口"
        align="left"
        prop="url"
        width="260px"
        show-overflow-tooltip
      />
      <el-table-column
        label="最后一次访问时间"
        align="left"
        prop="visitTime"
        width="180px"
        show-overflow-tooltip
      />
      <el-table-column
        label="访问量"
        align="left"
        prop="currentVisits"
        show-overflow-tooltip
      />
      <el-table-column
        label="方法"
        align="left"
        prop="method"
        show-overflow-tooltip
      />
      <el-table-column
        label="来源"
        align="left"
        prop="passSource"
        show-overflow-tooltip
      />
      <el-table-column label="资产状态" align="center" width="100px">
        <template slot-scope="scope">
          <el-tag
            v-if="scope.row.interfaceStatus === 1"
            type="primary"
            plain
            size="mini"
            >已上线
          </el-tag>
          <el-tag
            v-if="scope.row.interfaceStatus === 0"
            type="warning"
            plain
            size="mini"
            >已下线
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="活跃状态"
        align="center"
        fixed="right"
        width="100px"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <el-tag
            v-if="scope.row.isActive === 0"
            type="success"
            plain
            size="mini"
            >正常
          </el-tag>
          <el-tag
            v-if="scope.row.isActive === 1"
            type="warning"
            plain
            size="mini"
            >未知
          </el-tag>
          <el-tag v-if="scope.row.isActive === 2" type="info" plain size="mini"
            >僵尸
          </el-tag>
        </template>
      </el-table-column>
    </common-table>

    <pagination
      slot="pagination"
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </common-list>
</template>

<script>
import { getApiInterfaceList } from "@/api/apisecurity/apiinterface/interface";
import BaseList from "@/mixins/BaseList.vue";
import { PICKOPTIONS } from "@/utils/constant";
import dayjs from "dayjs";

export default {
  name: "index",
  mixins: [BaseList],
  data() {
    return {
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        beginTime: "",
        endTime: "",
        url: "",
      },
      isinfo: false,
      infoRow: {},
      total: 0,
      list: [],
      loading: false,
      date: [],
      pickerOptions: {
        shortcuts: PICKOPTIONS,
        //点击时，选择的是开始时间，也就是minDate
        onPick: ({ maxDate, minDate }) => {
          this.cuttentTime = minDate.getTime();
          if (maxDate) {
            //解除限制
            this.cuttentTime = "";
          }
        },
      },
      lastTime: [
        new Date().getTime() - 24 * 60 * 60 * 1000,
        new Date().getTime(),
      ],
    };
  },
  methods: {
    handleQuery() {
      this.queryParams.pageNum = 1;
      if (this.date.length) {
        this.queryParams.beginTime = dayjs(this.date[0]).format(
          "YYYY-MM-DD HH:mm:ss"
        );
        this.queryParams.endTime = dayjs(this.date[1]).format(
          "YYYY-MM-DD HH:mm:ss"
        );
      }
      this.getList();
    },
    resetQuery() {
      this.queryParams = this.$options.data().queryParams;
      this.queryParams.beginTime = "";
      this.queryParams.endTime = "";
      this.date = [];
    },
    getList() {
      this.loading = true;
      getApiInterfaceList(this.queryParams)
        .then((res) => {
          if (res && res.code === 200) {
            this.list = res.rows;
            this.total = res.total;
            this.handleListTips(res);
          } else {
            this.list = [];
            this.total = 0;
          }
        })
        .finally(() => {
          this.loading = false;
        });
    },
    handleInfo(row) {
      this.infoRow = row;
      this.isinfo = true;
    },
    closeDialog(row) {
      this.isinfo = false;
    },
    dateChange() {
      if (this.date && this.date.length > 0) {
        let time1 = this.date[0].getTime();
        let time2 = this.date[1].getTime();
        if (time2 - time1 > 31 * 24 * 3600 * 1000) {
          this.date = this.lastTime;
          return;
        }

        this.lastTime = this.date;
      }
    },
  },

  created() {
    this.getList();
  },
};
</script>
<style lang="scss" scoped></style>
